﻿using Report_Dal.Web;
using Report_IDal.Web;
using Report_IService.Web;
using Report_Model.Basic;
using Report_Model.Db;
using Report_Service.Basic;
using Report_Unity.Basic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Report_Service.Web
{
    public class S_Report_ItemConfigService : BaseService<S_Report_ItemConfig>, IS_Report_ItemConfigService
    {
        private IS_Report_ItemConfigDal T_Item_ConfigDal = new S_Report_ItemConfigDal(ConfigSource.tab_web);

        public override void SetCurrentDal()
        {
            base.BaseDal = T_Item_ConfigDal;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dept_name">科室名称</param>
        /// <param name="config_type"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public ResultInfo AddDept(string dept_name,string config_type,string userid)
        {
            if (string.IsNullOrEmpty(dept_name))
                return CodeStatus.error_201.GetStatus($"{nameof(dept_name)}必传");
            if (string.IsNullOrEmpty(config_type))
                return CodeStatus.error_201.GetStatus($"{nameof(config_type)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            S_Report_ItemConfig itemConfig = new S_Report_ItemConfig();
            itemConfig.id = GetNextId(a=>a.id);
            itemConfig.dept_name = dept_name;
            itemConfig.config_type = config_type;
            itemConfig.create_userid = userid;
            itemConfig.create_time = DateTime.Now;

            if (!Add(itemConfig))
                return CodeStatus.error_301.GetStatus("添加失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo EditDept(string id,string dept_name,string config_type,string userid)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(dept_name))
                return CodeStatus.error_201.GetStatus($"{nameof(dept_name)}必传");
            if (string.IsNullOrEmpty(config_type))
                return CodeStatus.error_201.GetStatus($"{nameof(config_type)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            S_Report_ItemConfig itemConfig = Get(a => a.id == id).FirstOrDefault();
            if (itemConfig == null)
                return CodeStatus.error_202.GetStatus("无配置信息");

            itemConfig.dept_name = dept_name;
            itemConfig.config_type = config_type;
            itemConfig.up_userid = userid;
            itemConfig.up_time = DateTime.Now;

            if (!Up(itemConfig))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo AddItem(string fid,string item_name,string config_type,string userid)
        {
            if (string.IsNullOrEmpty(fid))
                return CodeStatus.error_201.GetStatus($"{nameof(fid)}必传");
            if (string.IsNullOrEmpty(item_name))
                return CodeStatus.error_201.GetStatus($"{nameof(item_name)}必传");
            if (string.IsNullOrEmpty(config_type))
                return CodeStatus.error_201.GetStatus($"{nameof(config_type)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            S_Report_ItemConfig itemConfig = new S_Report_ItemConfig();
            itemConfig.id = GetNextId(a => a.id);
            itemConfig.item_name = item_name;
            itemConfig.fid = fid;
            itemConfig.config_type = config_type;
            itemConfig.create_userid = userid;
            itemConfig.create_time = DateTime.Now;

            if (!Add(itemConfig))
                return CodeStatus.error_301.GetStatus("添加失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo EditItem(string id, string item_name, string config_type, string userid)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(item_name))
                return CodeStatus.error_201.GetStatus($"{nameof(item_name)}必传");
            if (string.IsNullOrEmpty(config_type))
                return CodeStatus.error_201.GetStatus($"{nameof(config_type)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            S_Report_ItemConfig itemConfig = Get(a => a.id == id).FirstOrDefault();
            if (itemConfig == null)
                return CodeStatus.error_202.GetStatus("无配置信息");

            itemConfig.item_name = item_name;
            itemConfig.config_type = config_type;
            itemConfig.up_userid = userid;
            itemConfig.up_time = DateTime.Now;

            if (!Up(itemConfig))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo DelDeptOrItem(string id)
        {
            S_Report_ItemConfig itemConfig = Get(a => a.id == id).FirstOrDefault();
            if (itemConfig == null)
                return CodeStatus.error_202.GetStatus("无配置信息");

            List<S_Report_ItemConfig> listItemConfig = new List<S_Report_ItemConfig>();
            listItemConfig.Add(itemConfig);
            if (!string.IsNullOrEmpty(itemConfig.fid))
                listItemConfig.AddRange(Get(a => a.fid == id).ToList());

            if (!DelList(listItemConfig))
                return CodeStatus.error_301.GetStatus("删除失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo GetItemConfig()
        {
            List<S_Report_ItemConfig> list = GetAsNoTracking(a => true).ToList();

            List<object> listObj = new List<object>();
            List<S_Report_ItemConfig> listDept = list.Where(a => a.fid == null).ToList();
            foreach (S_Report_ItemConfig dept in listDept)
            {
               var listItem = list.Where(a => a.fid == dept.id).Select(a=>new
               {
                   id = a.id,
                   item_name = a.item_name,
                   config_type = a.config_type
               }).ToList();
               
                object obj = new { id = dept.id,dept_name = dept.dept_name, config_type = dept.config_type, list = listItem };
                listObj.Add(obj);
            }
            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = new { list = listObj};
            return res;
        }

        public ResultInfo GetItemConfigType()
        {
            ResultInfo res = CodeStatus.success.GetStatus();
            List<object> list = new ItemConfigTypeEnum().GetEnumType();
            res.data = new { list = list };
            return res;
        }
    }
}
